package com.wen.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wen.common.Result;
import com.wen.entity.*;
import com.wen.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 教练管理
 *
 * @author dhh
 * @date 2021/11/17 11:34
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/coach")
public class CoachController {

    private final UserService userService;

    private final CoachService coachService;

    private final MemberOrderService memberOrderService;

    private final EquipmentService equipmentService;

    private final ConsumRecordService consumRecordService;

    private final LeaveService leaveService;

    //    更新
    @PutMapping
    public Result<?> update(@RequestBody Coach coach) {
        coachService.updateById(coach);
        User user = new User();
        user.setId(coach.getUserId());
        user.setAge(coach.getAge());
        user.setPhone(coach.getPhone());
        user.setSex(coach.getSex());
        userService.updateById(user);
        return Result.success();
    }

    //    添加教练信息
    @PostMapping
    public Result<?> save(@RequestBody Coach coach) {
        User user = new User();
        user.setUsername(coach.getUserName());
        user.setPassword("123456");
        user.setAge(coach.getAge());
        user.setPhone(coach.getPhone());
        user.setSex(coach.getSex());
        user.setRole(3);
        userService.save(user);
        coach.setUserId(user.getId());
        coachService.save(coach);
        return Result.success();
    }

    //    s删除
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
        coachService.removeById(id);
        return Result.success();
    }

    //    查询
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Coach> NewsPage = coachService.page(new Page<>(pageNum, pageSize), null);
        return Result.success(NewsPage);
    }

    @GetMapping("/getCoachById")
    public Result<?> getCoachCopy(@RequestParam Integer coachId) {
        LambdaQueryWrapper<Coach> wrapper = Wrappers.<Coach>lambdaQuery();
        if (coachId != null) {
            wrapper.like(Coach::getId, coachId);
        }
        List<Coach> mapList = coachService.list(wrapper);
        return Result.success(mapList);
    }


    @GetMapping("/getCourseByCoachId")
    public Result<?> getCourseByCoachId(@RequestParam Integer coachId, @RequestParam String search) {

        List<Map<String, Object>> mapList = coachService.getCourseByCoachId(coachId, search);
        return Result.success(mapList);
    }

    @GetMapping("/getCount")
    public Result<?> findMember(@RequestParam String coachId) {

        Map map = new HashMap();

        // 学员人数
        Integer integer1 = consumRecordService.count(new LambdaQueryWrapper<ConsumRecord>().eq(ConsumRecord::getCoachId, coachId));
        map.put("count1", integer1);

        // 预约总数
        LambdaQueryWrapper<ConsumRecord> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(ConsumRecord::getCourseId, Integer.valueOf(coachId));
        List<ConsumRecord> consumRecords = consumRecordService.list(wrapper1);
        Integer integer = 0;
        if (CollectionUtil.isNotEmpty(consumRecords)) {
            List<Integer> collect = consumRecords.stream().map(ConsumRecord::getCourseId).collect(Collectors.toList());
            integer = memberOrderService.count(new LambdaQueryWrapper<MemberOrder>().in(MemberOrder::getCourseId, collect));
        }
        map.put("count2", integer);

        // 设备总数
        List<Equipment> equipment = equipmentService.list();
        int sum = 0;
        if (CollectionUtil.isNotEmpty(equipment)) {
            sum = equipment.stream().mapToInt(Equipment::getCount).sum();
        }
        map.put("count3", sum);

        //请假审批状态
        LambdaQueryWrapper<Leave> wrapper = Wrappers.<Leave>lambdaQuery();
        wrapper.eq(Leave::getCoachId, coachId);
        wrapper.orderByDesc(Leave::getStartDate);
        List<Leave> leavePage = leaveService.list(wrapper);
        Leave leave = new Leave();
        if (CollectionUtil.isNotEmpty(leavePage)) {
            leave = leavePage.get(0);
        }
        map.put("leave", leave);

        return Result.success(map);
    }
}
